package com.malamute.system.action.system;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import com.malamute.core.action.BaseAction;
import com.malamute.core.action.CrudMethod;
import com.malamute.core.common.SearchParamUtil;
import com.malamute.system.entity.NodeType;
import com.malamute.system.entity.TreeNode;
import com.malamute.system.service.TreeNodeService;

/**
 * @author Aceguan
 * @description 树节点Action
 * @date 2012-2-23 下午06:03:49
 * @version v1.0
 */
public class TreeNodeAction extends BaseAction implements CrudMethod {
	private TreeNode treeNode;
	private TreeNode belongTo;
	@Autowired
	private TreeNodeService treeNodeService;
	private Map<String, String> nodeTypeMap = new HashMap<String, String>();

	public void prepare() throws Exception {
		for (NodeType n : NodeType.values()) {
			nodeTypeMap.put(n.getKey(), n.getValue());
		}
	}

	public Map<String, String> getNodeTypeMap() {
		return nodeTypeMap;
	}

	public void setNodeTypeMap(Map<String, String> nodeTypeMap) {
		this.nodeTypeMap = nodeTypeMap;
	}

	public TreeNode getTreeNode() {
		return treeNode;
	}

	public void setTreeNode(TreeNode treeNode) {
		this.treeNode = treeNode;
	}

	public void prepareForm() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#save()
	 */
	@Override
	public String save() throws Exception {
		treeNodeService.saveTreeNode(treeNode);
		return SUCCESS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#update()
	 */
	@Override
	public String update() throws Exception {
		treeNodeService.updateTreeNode(treeNode);
		return ACTION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#list()
	 */
	@Override
	public String list() throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(TreeNode.class);
		SearchParamUtil.simpleSeach(criteria, searchParam);
		treeNodeService.listTreeNodesByPage(criteria, page);
		return LIST;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#search()
	 */
	@Override
	public String search() throws Exception {
		return this.list();
	}

	@Override
	// 新增or 删除
	public String form() throws Exception {
		// 父节点
		if (belongTo != null) {
			setBelongTo(treeNodeService.getTreeNodeById(belongTo.getId()));
		}
		// 当前节点
		if (treeNode != null) {
			setTreeNode(treeNodeService.getTreeNodeById(treeNode.getId()));
		}
		return FORM;
	}

	public String view() throws Exception {
		if (treeNode != null) {
			setTreeNode(treeNodeService.getTreeNodeById(treeNode.getId()));
		}
		return VIEW;
	}

	public TreeNode getBelongTo() {
		return belongTo;
	}

	public void setBelongTo(TreeNode belongTo) {
		this.belongTo = belongTo;
	}

}
